home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name pcenvchk -- Return an environment parameter specification
- *
- * Synopsis pparm = pcenvchk(pspec,len);
- * char *pparm The returned parameter
- * char *pspec The environment specification for which
- * to search
- * int len The maximum number of characters of the
- * parameter to return.
- *
- * Description This function searches the environment for the parameter
- * associated with the specfication pointed to by pspec. The
- * parameter is returned in the string pointed to pparm, but
- * no more than len characters are returned. The string pparm
- * is allocated space by PCENVCHK, and the space should be
- * freed by the calling function when no longer needed.
- * Only the first 127 characters of the environment are
- * searched. The environment specification must be specified
- * in upper case.
- *
- * Returns pparm Pointer to the parameter string
- *
- * Version 1.1 (C)Copyright Blaise Computing Inc. 1983, 1984
- *
- **/
- #include <compiler.h>
-
- #if LDATA
- #define NULL 0L
- #else
- #define NULL 0
- #endif
-
- struct segads /* Offset, segment address type */
- {
- unsigned r;
- unsigned s;
- };
- #define ADS struct segads /* Abbreviation */
-
- #if LAT200
- extern ADS _psp; /* Program segment prefix */
- #endif
- #if CI201A
- extern ADS _pspseg;
- #endif
- #if LAT104 | CI133D
- extern unsigned _pgmseg;
- #endif
-
- char *pcenvchk(pspec,len)
- char *pspec;
- int len;
- {
-
- ADS env_ads,env_loc;
- unsigned envseg,cs,ss,ds,es;
- char *penv_str,*pparm,*ptemp,*calloc();
- int len_env,len_spec,len_search,i,j,utslmove();
- #if CI201A & LDATA
- unsigned long ptrtoabs();
- #endif
-
- /* First move the first 128 bytes of the environment to a string. */
-
- #if LAT104 | CI133D
- env_loc.s = _pgmseg; /* The segment address of the */
- #endif /* environment is at offset 2C */
- #if LAT200 /* in the program segment prefix*/
- env_loc.s = _psp.s;
- #endif
- #if CI201A
- env_loc.s = _pspseg.s;
- #endif
- env_loc.r = 0x2c;
- #if LDATA
- #if CI201A
- env_ads.s = (unsigned)((ptrtoabs(&envseg) & 0xffff0L) >> 4L);
- env_ads.r = (unsigned)(ptrtoabs(&envseg) & 0xfL);
- #else
- env_ads.s = (unsigned)(((long)(&envseg) & 0xffff0L) >> 4L);
- env_ads.r = (unsigned)((long)(&envseg) & 0xfL);
- #endif
- #else
- utsreg(&cs,&ss,&ds,&es); /* Return segment reg values */
- env_ads.s = ds;
- env_ads.r = &envseg;
- #endif
- utslmove(&env_loc,&env_ads,2); /* envseg now has segment */
-
- penv_str = calloc(128,1); /* Put environment string here */
- ptemp = penv_str; /* Save pointer to storage */
- env_loc.s = envseg;
- env_loc.r = 0;
- #if LDATA
- #if CI201A
- env_ads.s = (unsigned)((ptrtoabs(penv_str) & 0xffff0L) >> 4L);
- env_ads.r = (unsigned)(ptrtoabs(penv_str) & 0xfL);
- #else
- env_ads.s = (unsigned)(((long)(penv_str) & 0xffff0L) >> 4L);
- env_ads.r = (unsigned)((long)(penv_str) & 0xfL);
- #endif
- #else
- env_ads.s = ds;
- env_ads.r = penv_str;
- #endif
- utslmove(&env_loc,&env_ads,128);
-
- /* Now determine the length of the environment. The environment */
- /* is terminated by two null bytes, so search for these. */
-
- len_env = 0;
- for (j = 0,len_env = 0; (j < 128) && (len_env == 0); j++)
- {
- if (*penv_str++ == 0)
- if (*penv_str++ == 0)
- len_env = j + 1;
- else
- j++;
- }
- penv_str -= len_env + 1; /* Point to beginning of string */
-
- /* Now search the environment string for the specification string */
-
- len_spec = strlen(pspec);
- len_search = len_env - len_spec + 1;
- for (i = 0; i < len_search; i += j + 1)
- {
- for (j = 0; (j < len_spec) && (*pspec++ == *penv_str++); j++);
- if (j == len_spec)
- break; /* penv_str points to parameter */
- pspec -= j + 1; /* search again */
- }
- if (i >= len_search)
- {
- free(ptemp);
- return(NULL); /* Not found in the environment */
- }
-
- /* Copy the parameter string to pparm. */
-
- pparm = calloc(len + 1,1);
- for (i = 0; (i < len) && (*pparm++ = *penv_str++); i++);
- pparm -= i + 1;
- free(ptemp); /* Release storage for penv_str */
-
- return(pparm);
-
- }